package com.zf.zfed.audit.ui

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.CheckBox
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R


@SuppressLint("ResourceAsColor", "RtlHardcoded")
class SetItem @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    // 图标
    private var itemIcon: ImageView
    
    // 名称
    private var itemName: TextView

    // 箭头 - 已移除
    // private var arrow: ImageView

    private var checkBox: CheckBox

    // 分割线 - 已移除
    // private var line: View

    init {
        isClickable = true
        isFocusable = true
        orientation = VERTICAL
        
        // 设置卡片背景
        background = ContextCompat.getDrawable(context, R.drawable.card_background)
        
        // 设置卡片边距
        val cardPadding = context.dpFromDimen(R.dimen.spacing_8)
        setPadding(cardPadding, cardPadding, cardPadding, cardPadding)

        // 内嵌布局
        val nestedFrameLayout = FrameLayout(context)
        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F)
        addView(nestedFrameLayout, layoutParams)

        // 图标
        itemIcon = ImageView(context)
        val iconSize = context.dpFromDimen(R.dimen.size_22)
        val iconLP = FrameLayout.LayoutParams(iconSize, iconSize)
        iconLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        iconLP.leftMargin = context.dpFromDimen(R.dimen.spacing_16)
        // 设置图标缩放类型
        itemIcon.scaleType = ImageView.ScaleType.CENTER_INSIDE
        nestedFrameLayout.addView(itemIcon, iconLP)

        // 文案
        itemName = TextView(context)
        itemName.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        itemName.textSize = context.spFromDimen(R.dimen.font_20)
        itemName.setTypeface(null, android.graphics.Typeface.BOLD)
        val textLP = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        textLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        textLP.leftMargin = context.dpFromDimen(R.dimen.spacing_50) // 为图标留出空间
        nestedFrameLayout.addView(itemName, textLP)

        // 箭头 - 已移除
        // arrow = ImageView(context)
        // arrow.background = ContextCompat.getDrawable(context, R.drawable.arrow)
        // val width = context.dpFromDimen(R.dimen.size_16)
        // val height = context.dpFromDimen(R.dimen.size_16)
        // val imageLP = FrameLayout.LayoutParams(width, height)
        // imageLP.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        // imageLP.marginEnd = context.dpFromDimen(R.dimen.spacing_16)
        // nestedFrameLayout.addView(arrow, imageLP)

        // 单选框
        checkBox = CheckBox(context)
        checkBox.setButtonDrawable(R.drawable.check_selector)
        checkBox.isClickable = false
        checkBox.visibility = GONE
        val checkBoxLP =
            FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        checkBoxLP.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        checkBoxLP.marginEnd = context.dpFromDimen(R.dimen.spacing_16)
        nestedFrameLayout.addView(checkBox, checkBoxLP)

        // 分割线 - 已移除
        // line = View(context)
        // line.setBackgroundColor(ContextCompat.getColor(context, R.color.solid_line))
        // val lineLP = LayoutParams(LayoutParams.MATCH_PARENT, context.dpFromDimen(R.dimen.size_1))
        // lineLP.leftMargin = context.dpFromDimen(R.dimen.spacing_16)
        // addView(line, lineLP)

        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SetItem)
            val title = typedArray.getString(R.styleable.SetItem_itemString)
            val iconRes = typedArray.getResourceId(R.styleable.SetItem_itemIcon, 0)
            val showCheck = typedArray.getBoolean(R.styleable.SetItem_showCheck, false)
            
            itemName.text = title
            if (iconRes != 0) {
                itemIcon.setImageResource(iconRes)
                itemIcon.visibility = VISIBLE
                // 设置图标颜色为白色
                itemIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
            } else {
                itemIcon.visibility = GONE
            }
            
            if (showCheck) {
                checkBox.visibility = VISIBLE
            } else {
                checkBox.visibility = GONE
            }
            typedArray.recycle()
        } else {
            // 默认设置
            itemIcon.visibility = GONE
            checkBox.visibility = GONE
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    fun setTitle(title: String) {
        itemName.text = title
    }

    /**
     * 设置图标
     *
     * @param iconRes
     */
    fun setIcon(iconRes: Int) {
        if (iconRes != 0) {
            itemIcon.setImageResource(iconRes)
            itemIcon.visibility = VISIBLE
        } else {
            itemIcon.visibility = GONE
        }
    }

    /**
     * 更细选中状态
     *
     * @param b
     */
    fun checked(b: Boolean) {
        checkBox.isChecked = b
    }
}
fun Context.spFromDimen(id: Int): Float {
    return resources.getDimension(id) / resources.displayMetrics.scaledDensity
}

fun Context.dpFromDimen(id: Int): Int {
    return resources.getDimensionPixelSize(id)
}